package xdu.lz.netty.f_netty_buf;

import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.buffer.UnpooledHeapByteBuf;
import io.netty.util.ReferenceCounted;
import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Iterator;

import io.netty.buffer.ByteBuf;

/**
 * @author lizhen
 * @date 2019/8/19 20:37
 * @desc
 */
public class ByteBuf_ {

    @Test
    public void test1() {
        ByteBuf buffer = Unpooled.buffer(10);
        for (int i = 0; i < buffer.capacity(); i++) {
            buffer.writeByte(i); //相对方法，writePoint会++
        }
        byte[] array = buffer.array();
        System.out.println(Arrays.toString(array));
        for (int i = 0; i < buffer.capacity(); i++) {
            System.out.println(buffer.readByte());; //相对方法，readPoint会 ++
            System.out.println(buffer.getByte(i));;  //绝对方法，readPoint不会改变
        }
    }

    /**
     * 2 copiedBuffer  readerIndex   getCharSequence
     *
     */
    @Test
    public void test2() throws Exception {
            //拷贝一个数组，放在heap 内存
        ByteBuf byteBuf = Unpooled.copiedBuffer("你好hello", Charset.forName("utf8"));
        if (byteBuf.hasArray()){//判断是否是堆内存， 如果背后是数组则是堆内存
            byte[] array = byteBuf.array();
            System.out.println(new String(array,"utf8"));//utf8一个中文3字节
            System.out.println(byteBuf);
            /**
             * 注意这里初始化的长度不是字符串的所占字节的长度，而是有一个计算公式
             * 为了预留位置方便写操作
             */
            //int length = (int) ((double) src.remaining() * encoder.maxBytesPerChar()) + extraCapacity;
            System.out.println(byteBuf.capacity());

            System.out.println("byteBuf.readerIndex:"+byteBuf.readerIndex());
            System.out.println("byteBuf.writerIndex"+byteBuf.writerIndex());
            System.out.println((char)byteBuf.readByte());
            System.out.println(byteBuf.readerIndex());
            byteBuf.readerIndex(0);
            System.out.println((char)byteBuf.readByte());
            System.out.println("byteBuf.readableBytes() = " + byteBuf.readableBytes());// wirterindex - readinder

            /**
             * 一串字符 ：现在直接获取 bytebuf【0】乱码，因为中文占3字节
             */
            //截取字符串序列
            System.out.println(byteBuf.getCharSequence(0,6,Charset.forName("utf8")));
        }
    }

    /**
     * 3 compositeBuf:复合缓冲区： 底层是ArrayList，可以容纳 heapBuf或directBuf
     * 	应用场景：如http  头使用heap  ， 体使用direct  ， 则可以放入compositebuf，类似scatter和gather
     */
    @Test
    public void test3(){

        CompositeByteBuf compositeByteBuf = Unpooled.compositeBuffer();
        ByteBuf heapBuf = Unpooled.buffer(10);// heapBuf
        ByteBuf directBuf = Unpooled.directBuffer(8);//directBuf

        compositeByteBuf.addComponents(heapBuf,directBuf);

//        compositeByteBuf.removeComponent(0);

        //遍历方式1
        for (ByteBuf buf : compositeByteBuf) {
            System.out.println(buf);
        }
        //方式2
//        Iterator<ByteBuf> iterator = compositeByteBuf.iterator();
        //方式3
        compositeByteBuf.forEach(buf ->{
            System.out.println(buf);
        });
        compositeByteBuf.forEach(System.out::println);

        System.out.println(int.class);
    }

}
